import {ipc} from '@/utils/ipcRenderer'
import {ipcApiRoute} from "@/api/router";
import type {App, Plugin} from "vue";
import storage from 'store2'

const globalPlugin: Plugin = {
  install: (app: App, options: Object) => {
    // 引入ipc，确保界面和electron业务层通信
    app.config.globalProperties.$ipc = ipc

    // 获取http host
    app.config.globalProperties.$getHttpHost = () => {
      return new Promise((resolve, reject) => {
        ipc.invoke(ipcApiRoute.checkHttpServer, {}).then((res: { enable: any; server: string; }) => {
          if (res.enable) {
            storage.set('httpServiceConfig', res);
            resolve(res.server || 'http://localhost:7071')
          } else {
            reject('http服务未启用！')
          }
        })
      })
    }
  }
}
export default globalPlugin
